package com.joyance.test_servlet.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.CollectionUtils;


public class TestServletContext extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;


    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // ServletContext
        System.out.println("ServletContext================");
        ServletContext servletContext = super.getServletContext();
        // getAttribute
        Object username = servletContext.getAttribute("username");
        System.out.println("request.getAttribute(\"username\")=" + req.getAttribute("username"));
        System.out.println("servletContext.getAttribute(\"username\")=" + username);
        // getContext
        ServletContext sc = servletContext.getContext("/test_servlet/forward");
        Object password = sc.getAttribute("password");
        System.out.println("servletContext.getContext(\"/test_servlet/forward\").getAttribute(\"password\")=" + password);
        // getMajorVersion
        System.out.println("servletContext.getMajorVersion()=" + sc.getMajorVersion());
        // getMinorVersion
        System.out.println("servletContext.getMinorVersion()=" + sc.getMinorVersion());
        // getMimeType
        ServletContext context = getServletConfig().getServletContext();
        String mimeType = context.getMimeType("/WEB-INF/web.xml");
        System.out.println("servletContext.getMimeType()=" + mimeType);
        // getRealPath
        String realPath = sc.getRealPath("test");
        System.out.println("servletContext.getRealPath()=" + realPath);
        // getResource
        URL url = sc.getResource("index.html");
        System.out.println("servletContext.getResource()=" + url.getPath());
        // getResourceAsStream
        InputStream inputStream = sc.getResourceAsStream("index.html");
        byte[] bytes = new byte[1024];
        List<Byte> list = new ArrayList<Byte>();
        while (inputStream.read(bytes) != -1) {
            CollectionUtils.mergeArrayIntoCollection(bytes, list);
        }
        String str = new String(convertListToArray(list));
        System.out.println("sc.getResourceAsStream(\"index.html\"):\n" + str);
        // getServerInfo
        String servletInfo = sc.getServerInfo();
        System.out.println("sc.getServerInfo()=" + servletInfo);
        // log
        sc.log("==========++++++++++");
        // getInitParameter
        String globalName = sc.getInitParameter("globalName");
        System.out.println("sc.getInitParameter(\"globalName\")=" + globalName);
    }


    private byte[] convertListToArray(List<Byte> list) {
        byte[] bytes = new byte[list.size()];
        int index = 0;
        for (Byte b : list) {
            bytes[index++] = b;
        }
        return bytes;
    }

}
